<?php

namespace Home\Controller;
use Think\Page;


/**
 * 学生管理控制器
 * @author水月居 <singliang@163.com>
 */
class StudentController extends HomeController {

    /**
     * 用户管理首页
    */
    public function index(){
		
		//echo I('class');
		$sclass = I('class')?I('class'):"请选择班级" ; //设置学生班级
		$where=I('class')?array("class"=>I('class'),"status"=>"1"):array("status"=>"1");
        $student=D("student");
        $list=$student->relation(true)->order("sid asc")->where($where)->limit(45)->select();
		//$list=$student->field("sid, grade, class, code, name, sex, cid, status")->order("sid asc")
		//		->where($where)->limit(45)->select();
		$class=M('class')->field("class")->select();
	    $this->assign('_sclass',$sclass);
		$this->assign('_class', $class);
        $this->assign('_list', $list);
        //dump($list);
        $this->meta_title = '学生信息';
        $this->display();
    }
    /*
    **查看未报名学生名单*
    */
    public function showStudent0(){
    //获取课程信息
        $curriculum=D("Curriculum");
        $clist=$curriculum->field("cid, cname")->select();
        //获取学生信息
         $map['cid'] = 0;//获取cid
        $student=D('student');
        $order=array('grade'=>'desc','sid'=>'asc');       
        $stlist=$student->field("sid,grade,code,class,name,sex,update_time")->order($order)->where($map)->limit('30')->select();
        //$stlist=$student->where($map)->select();
        //echo $curriculum->getLastSql();
        //dump($stlist);
        //获取当前课程信息
        $cur=$curriculum->where($map)->find();
        // dump($cur);
        $this->assign('_cur', $cur);
        $this->assign('_curriculum', $clist);
        $this->assign('student', $stlist);
        //输出sseion
        //dump($_SESSION);
        $this->display('showStudent');  

    }
    /*查看学生报名情况*/
	    public function showStudent(){
    //获取课程信息
        $curriculum=D("Curriculum");
        $clist=$curriculum->field("cid, cname")->select();
        //获取学生信息
         $map['cid'] = I('cid');//获取cid
        $student=D('student');
        $order=array('grade'=>'desc','sid'=>'asc');
       
        $stlist=$student->field("sid,grade,code,class,name,sex,update_time")->order($order)->where($map)->limit('30')->select();
        //$stlist=$student->where($map)->select();
        //echo $curriculum->getLastSql();
        //dump($stlist);
        //获取当前课程信息
        $cur=$curriculum->where($map)->find();
        // dump($cur);
        $this->assign('_cur', $cur);
        $this->assign('_curriculum', $clist);
        $this->assign('student', $stlist);
        //输出sseion
        //dump($_SESSION);
        $this->display();  

    }
        /**
     * 修改学生课程信息初始化
     * @author singliang <singliang@163.com>
     */
    
    public function select_curriculum(){
        $student = M('student')->find(I('sid'));
        $this->assign('student', $student);
        $map['grade'] =array('like',"%".$student['grade']."%");
		$map['year'] = C('YEAR');
		$map['status'] = 1;
        $curriculum=M('curriculum')->where($map)->limit(50)->select();
        
        //$curriculum=$sql->getLastSql();
        //dump($curriculum);
        $this->assign('curriculum', $curriculum);
        $this->assign('student', $student);
        $this->meta_title = '学生选课';
        $this->display();
    }
    public function truncate(){
                $data = array(
            'sid'             => I('sid'),
            'cid'             => 0,
            'update_time' => NOW_TIME,
            'update_ip'   => get_client_ip(1),
        );

         $cur['class']=I('class');
         $res = D('student')->save($data);

        if(!$res){
            $this->error(D('student')->getError());
        }else{
            $this->success('清除成功！', U('Student/index',$cur));
        }

    }
	//保存选课程信息
	 public function saveStudentCurriculum(){

        $data = array(
            'sid'             => I('sid'),
            'cid'             => I('cid'),
            'update_time' => NOW_TIME,
            'update_ip'   => get_client_ip(1),
        );

           $cur['class']=I('class');
           $res = D('student')->save($data);
           
        if(!$res){
            $this->error(D('student')->getError());
        }else{
			$curr=M('curriculum');
			$curdata['cid']=I('cid');
			$curdata['count']=M('student')->where($curdata)->count();
		  	$curr->save($curdata);
			//dump($curdata);
		
            $this->success('选课成功！', U('Student/index',$cur));
        }
    }
}